package com.ttn.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;

import com.ttn.entities.User;
import com.ttn.user.bus.UserService;

@Component
public class UserValidator  {
	
	@Autowired
	private UserService userService;

	 public void validateRegistrationForm(User user, ValidationContext context) {	
		 
		  if (!userService.getUserByLogin(user.getLogin()).isEmpty()){
			  MessageContext messages = context.getMessageContext();
			  messages.addMessage(new MessageBuilder().error().source("registrationForm:login").code("login.not.unique").
		                defaultText("Login already exits").build());
			 
		  }	
	}

}
